﻿using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Windows.Markup;
using nRoute.Internal;

namespace nRoute.Components
{
#if (!SILVERLIGHT)
    [Serializable]
#endif
    [DataContract]
    [ContentProperty("Value")]
    [KnownType(typeof(Parameter))]
    public class Parameter 
        : IEquatable<Parameter>
    {
        private const string KEY_SETONCE_ONLY = "Parameter's key can only set once";

        private string _key;
        private object _value;

        public Parameter() { }

        public Parameter(string key, Object value)
        {
            this.Key = key;
            this.Value = value;
        }

#region Properties

        [DataMember]
        [Category("Common Properties")]
        public string Key
        {
            get { return _key; }
            set
            {
                Guard.ArgumentValue(_key != null, "Key", KEY_SETONCE_ONLY);
                _key = value;
            }
        }

        [DataMember]
        [Category("Common Properties")]
        public Object Value
        {
            get { return _value; }
            set { _value = value; }
        }

#endregion

#region IEquatable<Parameter> Members

        public bool Equals(Parameter other)
        {
            if (other == null) return false;
            return string.Equals(Key, other.Key, StringComparison.InvariantCultureIgnoreCase);
        }

#endregion

    }
}

// private static readonly ParameterComparer _comparer = new ParameterComparer();

//#region Static Property

//        public static IEqualityComparer<Parameter> Comparer
//        {
//            get { return _comparer; }
//        }

//#endregion

//#region Nested Class

//        private class ParameterComparer
//            : IEqualityComparer<Parameter>
//        {
//            public ParameterComparer() { }

//#region IEqualityComparer<Parameter> Members

//            public bool Equals(Parameter x, Parameter y)
//            {
//                if (x == null && y == null) return true;
//                return x == null ? y.Equals(x) : x.Equals(y);
//            }

//            public int GetHashCode(Parameter obj)
//            {
//                Guard.ArgumentNotNull(obj, "obj");
//                return obj.Key == null ? 
//                    string.Empty.GetHashCode() : obj.Key.ToUpper(CultureInfo.InvariantCulture).GetHashCode();
//            }

//#endregion

//        }

//#endregion